﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using WinnerCMS.Common;
using WinnerCMS.Data;
using System.Linq;
using WinnerCMS.Model;
using WinnerCMS.Model.Enum;

#endregion

namespace WinnerCMS.BLL.PageFactory.ManageHtml
{
    /// <summary>
    /// 删除HTML
    /// </summary>
    /// <remarks>禁止在多线程中调用</remarks>
    public static class DeleteHTML
    {
        #region 删除内容页

        /// <summary>
        /// 删除内容页
        /// </summary>
        /// <param name="id"></param>
        public static void ContentPage(int id)
        {
            if (HttpContext.Current.Request["ModelId"] == null)
            {
                Core model = new CoreManage().GetBaseInfo(id);
                if (model == null)
                {
                    return;
                }

                //如果不知道是什么模型，那么只能靠猜测来删除
                string filePath = StaticFilePath.ContentShowUrl(model);
                FileInfo fi = new FileInfo(filePath);
                FileInfo[] filePaths = fi.Directory.GetFiles(fi.Name + "*", SearchOption.TopDirectoryOnly);
                filePaths.ForEach(x => x.Delete());
            }
            else
            {
                byte ModelId = byte.Parse(HttpContext.Current.Request["ModelId"]);
                ContentPage(id, ModelId);
            }
        }

        #endregion

        #region 删除内容页

        /// <summary>
        /// 删除内容页
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ModelId"></param>
        private static void ContentPage(int id, byte ModelId)
        {
            dynamic model = new CoreManage().GetBaseInfo(ModelId, id);
            if (model == null)
            {
                return;
            }
            if (ModelType.Article == ModelManage.GetBaseModel(ModelId))
            {
                int PageCount = Pages.ArticleCommon.ArticlePageCount(model.Content);

                for (int i = 1; i < PageCount + 1; i++)
                {
                    string FilePath = StaticFilePath.ContentShowUrl(model, i);

                    FilePath = HttpServer.MapPath(FilePath);

                    try
                    {
                        File.Delete(FilePath);
                    }
                    catch
                    {
                    }
                }
            }
            else
            {
                string FilePath = StaticFilePath.ContentShowUrl(model);

                FilePath = HttpServer.MapPath(FilePath);

                try
                {
                    File.Delete(FilePath);
                }
                catch
                {
                }
            }
        }

        #endregion
    }
}